<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Loader extends CI_Loader 
{
    protected $_my_controller_paths     = array();  
    protected $_my_controllers          = array();
	var $_ci_is_inside_module = false;  // 当前是否是 Module 里的 Loader
	
 
    public function __construct()
    {
        parent::__construct();
 
        $this->_my_controller_paths = array(APPPATH);
    }
 
    public function controller($controller, $name = '', $db_conn = FALSE)
    {
        if (is_array($controller))
        {
            foreach ($controller as $babe)
            {
                $this->controller($babe);
            }
            return;
        }
 
        if ($controller == '')
        {
            return;
        }
 
        $path = '';
 
        // Is the controller in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($controller, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($controller, 0, $last_slash + 1);
 
            // And the controller name behind it
            $controller = substr($controller, $last_slash + 1);
        }
 
        if ($name == '')
        {
            $name = $controller;
        }
 
        if (in_array($name, $this->_my_controllers, TRUE))
        {
            return;
        }
 
        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The controller name you are loading is the name of a resource that is already being used: '.$name);
        }
 
        $controller = strtolower($controller);
		$controller=ucfirst($controller);//ci3之后强制要求首字母大写了
        foreach ($this->_my_controller_paths as $mod_path)
        {
            if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php'))
            {
                continue;
            }
 
            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }
 
                $CI->load->database($db_conn, FALSE, TRUE);
            }
 
            if ( ! class_exists('CI_Controller'))
            {
                load_class('Controller', 'core');
            }
 
            require_once($mod_path.'controllers/'.$path.$controller.'.php');
 
            $controller = ucfirst($controller);
 
            $CI->$name = new $controller();
 
            $this->_my_controllers[] = $name;
            return;
        }
 
        // couldn't find the controller
        show_error('Unable to locate the controller you have specified: '.$controller);
    }
	
	
	/**
     * Model Loader
     *
     * This function lets users load and instantiate models.
     *
     * @access  public
     * @param   string  the name of the class
     * @param   string  name for the model
     * @param   bool    database connection
     * @return  void
     */
    function model($model, $name = '', $db_conn = FALSE)
    {
        if (is_array($model))
        {
            foreach ($model as $babe)
            {
                $this->model($babe);
            }
            return;
        }

        if ($model == '')
        {
            return;
        }

        $path = '';

        // Is the model in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($model, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($model, 0, $last_slash + 1);

            // And the model name behind it
            $model = substr($model, $last_slash + 1);
        }

        if ($name == '')
        {
            $name = $model;
        }

        if (in_array($name, $this->_ci_models, TRUE))
        {
            return;
        }

        $CI =& get_instance();

        $model_paths = $this->_ci_model_paths;

        if ($this->_ci_is_inside_module)
        {
            $module_class_name = $this->_ci_module_class;
            array_unshift($model_paths, APPPATH.'modules/'.$this->_ci_module_path.'/');
            $module_model_name = str_replace(' ', '_', ucwords(str_replace('/', ' ', $this->_ci_module_path.' '.$model)));
            if (isset($CI->$module_class_name->$name))
            {
                show_error('The model name you are loading is the name of a resource that is already being used: '.$module_class_name.'.'.$module_model_name);
            }
        }
        else
        {
            if (isset($CI->$name))
            {
                show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
            }
        }

        //$model = strtolower($model);

        foreach ($model_paths as $key=>$mod_path)
        {
            if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
            {
                continue;
            }

            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }

                $CI->load->database($db_conn, FALSE, TRUE);
            }

            if ( ! class_exists('CI_Model'))
            {
                load_class('Model', 'core');
            }

            require_once($mod_path.'models/'.$path.$model.'.php');

            $model = ucfirst($model);

            if ($this->_ci_is_inside_module)
            {
                // 一定要放到全局 loader 实例中，否则还是无法查询模型属于哪个模块
                $CI->load->_ci_module_models[$module_model_name] = $module_class_name;

                if ($key == 0)
                {
                    $CI->$module_class_name->$name = new $module_model_name();
                }
                else
                {
                    $CI->$module_class_name->$name = new $model();
                }
            }
            else
            {
                $CI->$name = new $model();
            }

            $this->_ci_models[] = $name;
            return;
        }

        // couldn't find the model
        show_error('Unable to locate the model you have specified: '.$model);
    }
 
}
 